package xyz.luhui1hao.springboottest.web;

import org.apache.shiro.session.mgt.SessionContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import xyz.luhui1hao.springboottest.domain.User;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 这个主要用来测试SpringMVC的各种注解用法
 */
@RestController
@RequestMapping("/springmvc")
public class SpringMVCTestController {


    //***************************************************************************************************************************
    /**
     * ModelAttribute加在有返回值的方法上
     * 跟加在无返回值上的区别只是，它会把返回的值add到Model中
     * @param model
     * @return
     */
    @ModelAttribute(value = "myUser")
    public User test(Model model){
        model.addAttribute("test", "123");
        User user = new User();
        user.setName("luhui");
        return user;
    }

    /**
     *可以用于数据初始化
     * @param model
     * @return
     */
    @RequestMapping(value = "/testModelAttribute1", method = RequestMethod.POST)
    public String testModelAttribute1(Model model){
        StringBuilder sb = new StringBuilder();
        Map map = model.asMap();
        sb.append("test = " + map.get("test"));
        sb.append("\n");
        sb.append("myUser = " + map.get("myUser"));

        return sb.toString();
    }

    /**
     * 可以用于获取表单提交的数据
     * @param user
     * @param model
     * @return
     */
    @RequestMapping(value = "/testModelAttribute2", method = RequestMethod.POST)
    public String testModelAttribute2(@ModelAttribute("name") User user, Model model){
        StringBuilder sb = new StringBuilder();
        sb.append("user = " + user);
        sb.append("\n");
        sb.append("************************************");
        sb.append("\n");
        sb.append(model.asMap().get("test"));
        sb.append("\n");
        sb.append(model.asMap().get("myUser"));

        return sb.toString();
    }

    //***************************************************************************************************************************

    /**
     * 获取Cookie中的值
     * @param sessionId
     * @return
     */
    @RequestMapping("/cookie")
    public String displayHeaderInfo(@CookieValue("JSESSIONID") String sessionId)  {
        if(sessionId == null){
            return "创建了一个新的session";//事实上不会有null，因为为null的话会被Shiro拦截，重定向到login页面
        }else{
            return "cookie中的sessionId为"+sessionId;
        }
    }

    /**
     * 在session域中存值
     *
     * Session对象会由程序根据Cookie中的JSESSIONID自动创建或获取,直接用就行
     * @param session
     * @return
     */
    @RequestMapping(value = "/saveSession", method = RequestMethod.POST)
    public String saveSessionValue(HttpSession session){
        Integer count= (Integer)session.getAttribute("count");
        if(count == null){
            count = 1;
        }else {
            count++;
        }

        session.setAttribute("count", count);

        return "count = " + count;
    }
}
